from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
def main_draw(excel='ATL双向_JobId1717224.xlsx',csv='全球机场经纬度.csv',picturename='亚特兰大（ATL）进港航线图'):
    
# 读入数据
	data=pd.read_excel(excel)
	# 把机场名字写成参数，供后续修改函数使用。
	AP_name='ATL'
	# 数据初始化，前面NaN数据删除，
	data_clear= data.loc[22:7318]
	# 更改列表头。
	data_clear.columns=['Carrier Code','Carrier Name','Routing','International/Domestic','O&D Pair (Airport)','O&D Pair (City)',              'O&D Pair (Country)','O&D Pair (Region)','O&D Pair (State)','Frequency','Seats (Total)','ASKs','Time series']
	data_clear
	data_clear['O&D Pair (Airport)'].unique()
	len(data_clear['O&D Pair (Airport)'].unique())
	# 选取data_clear中的有用信息，拆分起飞机场和降落机场。
	data_used=data_clear[['O&D Pair (Airport)','Frequency']]
	data_used.loc[:,'depAP']=data_used['O&D Pair (Airport)'].str[0:3]
	data_used.loc[:,'arrAP']=data_used['O&D Pair (Airport)'].str[-3:7]
	# 选择起飞机场是AP_name的机场。
	data_depAP=data_used[data_used['depAP']==AP_name][['arrAP','Frequency']]
	# 不同航司的航班频次需要累加。
	data_depAP_ALL=data_depAP.groupby('arrAP').sum()
	data_depAP_ALL
	# 原始数据有机场重复问题，已经解决，采用.csv文件读取速度更快。
	AP=pd.read_csv(csv)
	# 修改AP的主键，方便后续两个表格合并。
	AP.index=AP['AP']

	'''a=pd.merge(data_depAP, AP, how='left', on=data_depAP.index, left_on=None, right_on=None,
	         left_index=False, right_index=False, sort=True,
	         suffixes=('_x', '_y'), copy=True, indicator=False,
	         validate=None)'''
	#3.当两个表中，多个主键名称不一样时，主键合并，除显示主键中一样的参数外其他值也显示没有的表中用NaN代替
	#pd.merge(left, right, how='outer', left_on='key2',right_on='key1')
	#pd.merge(l, right, how='outer', left_on='key2',right_on='key1')

	# 作图用基本数据表如下。
	data_plot=data_depAP_ALL.join(AP)
	data_plot.head()

	names = data_plot['AP'].values.tolist()
	fres  = data_plot['Frequency'].values.tolist()
	lats  = data_plot['LAT'].values.tolist()
	lons  = data_plot['LONG'].values.tolist()

	plt.figure(figsize=(16,8)) 
	map = Basemap(projection='mbtfpq',lat_0=0,lon_0=-20,resolution='l')  # mbtfpq 麦克布赖德-托马斯平极四次投影。

	map.drawcoastlines(color='gray',linewidth=0.5)
	map.drawcountries(linewidth=0.25)
	# draw the edge of the map projection region (the projection limb)
	map.drawmapboundary(fill_color='#689CD2')
	# draw lat/lon grid lines every 30 degrees.
	map.drawmeridians(np.arange(0,360,30))
	map.drawparallels(np.arange(-90,90,30))
	# Fill continent wit a different color
	map.fillcontinents(color='#FFDDCC',lake_color='#689CD2',zorder=0)
	# compute native map projection coordinates of lat/lon grid.
	x, y = map(lons, lats)
	max_fres = sum(fres)
	# Plot each city in a loop.
	# Set some parameters
	size_factor = 500.0
	y_offset    = 15.0
	rotation    = 30

	for i,j,k,name in zip(x,y,fres,names):
	    size = size_factor*k/max_fres
	    cs = map.scatter(i,j,s=size,marker='o',color='black')
	    #plt.text(i,j+y_offset,name,rotation=rotation,fontsize=8)
	for i in range(len(x)):
	    map.drawgreatcircle(lons[i],lats[i],-84.43,33.64,del_s=100.0,linewidth=fres[i]/1000,color='#FF5600')
	#map.drawgreatcircle(lons[1],lats[1],-84.43,33.64,del_s=10.0,linewidth=2)

	plt.title(picturename,fontsize=18)
	plt.tight_layout()
	plt.show()

if __name__ == '__main__':
	main_draw()
# In[ ]:




